package programm;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JSpinner;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SpinnerNumberModel;

import jniGlue.Librarylinker;
import javax.swing.JLabel;
import java.awt.Font;

public class V_dmxSlider extends JPanel implements ActionListener, ChangeListener{

	private static final long serialVersionUID = 1L;
	private Librarylinker fx5;
	private JButton flashbutton;
	private JSpinner channelspinner;
	private JSlider valueslider;
	private int channelvaluearray[] = new int[512];
	private JPanel startPanel;
	private JPanel panel;
	private JLabel lblPjdmxValueslider;
	
	/**
	 * Konstruktor
	 */
	public V_dmxSlider(final Librarylinker currentos) {
		
		fx5 = currentos;
		
		this.setBounds(100, 100, 450, 300);
		this.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.setLayout(new BorderLayout(0, 0));
		
		startPanel = new JPanel();
		add(startPanel, BorderLayout.NORTH);
		startPanel.setLayout(new BorderLayout(0, 0));
		
		panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		
		lblPjdmxValueslider = new JLabel("PJ-DMX: ValueSlider");
		lblPjdmxValueslider.setFont(new Font("Dialog", Font.BOLD, 16));
		panel.add(lblPjdmxValueslider);
		
		JPanel channelpanel = new JPanel();
		add(channelpanel, BorderLayout.CENTER);
		channelpanel.setLayout(new BorderLayout(0, 0));
		
		valueslider = new JSlider();
		valueslider.setValue(0);
		valueslider.setPaintTicks(true);
		valueslider.setPaintLabels(true);
		valueslider.setOrientation(SwingConstants.VERTICAL);
		valueslider.setMinorTickSpacing(10);
		valueslider.setMaximum(255);
		valueslider.setMajorTickSpacing(50);
		valueslider.addChangeListener(this);
		channelpanel.add(valueslider, BorderLayout.NORTH);
		
		JPanel channelbuttonpanel = new JPanel();
		add(channelbuttonpanel, BorderLayout.SOUTH);
		
		flashbutton = new JButton("FLASH");
		flashbutton.addActionListener(this);
		
		channelbuttonpanel.add(flashbutton);
		
		channelspinner = new JSpinner();
		channelspinner.setModel(new SpinnerNumberModel(1, 1, 512, 1));
		channelspinner.addChangeListener(this);
		channelbuttonpanel.add(channelspinner);
		
		//set all values to 0
		for(int i = 0; i<512; i++)
		{
			channelvaluearray[i] = 0;
			fx5.senddmx(i, 0);
		}
	}
	
	
	public void actionPerformed(ActionEvent event) {
		int channel = Integer.valueOf(channelspinner.getValue().toString()) -1; //von 0 bis 511
		
		//flash button sets the channel to maximum and then resets to the old value
		if(event.getSource() == flashbutton)
		{
			fx5.senddmx(channel, 255);
			try{
				  Thread.currentThread();
				  Thread.sleep(100);//sleep for 100 ms
				}
			catch(InterruptedException ie){
				System.out.println("Thread sleeping error: "+ie);
				}
			fx5.senddmx(channel, channelvaluearray[channel]);
		}
		else
			System.out.println("Für dieses ActionEvent ist nichts hinterlegt: " + event.getSource());
		}
	
	
	public void stateChanged(ChangeEvent event) { 
		int channel = Integer.valueOf(channelspinner.getValue().toString()) -1; //from 0 to 511

		//if you use the slider, then the value has to be saved in the array
		if(event.getSource() == valueslider)
		{
			channelvaluearray[channel] = valueslider.getValue();
			fx5.senddmx(channel, valueslider.getValue());
		}
		//if you change the channel, then the saved value of the new channel should be used
		else if(event.getSource() == channelspinner)
		{
			valueslider.setValue(channelvaluearray[channel]);
		}
		else
			System.out.println(event.getSource());
	}
}
